#include "SafeQue.hpp"

#define PTHREAD_SIZE 3 
SafeQue que;

void* Pro_Start(void* arg)
{
    int id = *((int*) arg);
    int val = 1;
    while(val <= 100){
        que.Push(val, id);
        val++;
    }
    return NULL;
}

void* Cons_Start(void* arg)
{
    int id = *((int*) arg);
    while(1){
        int ret = que.Pop(id);
        if(ret == 100){
            break;
        }
    }
    return NULL;
}

int main()
{
    pthread_t pro[PTHREAD_SIZE];
    pthread_t cons[PTHREAD_SIZE];
    for(int i = 0;  i < PTHREAD_SIZE; ++i){
        int* tmp = new int(i+1);
        int ret = pthread_create(&pro[i], NULL, Pro_Start, tmp);
        if(ret != 0){
            perror("pthread_create");
            return 0;
        }
        
        ret = pthread_create(&cons[i], NULL, Cons_Start, tmp);
        if(ret != 0){
            perror("pthread_create");
            return 0;
        }
    }

    for(int i = 0; i < PTHREAD_SIZE; ++i){
            pthread_join(pro[i], NULL);
            pthread_join(cons[i], NULL);
    }
    return 0;
}
